#!/usr/bin/env python
#
# Copyright (C) 2011 Clement Lorteau <clem@lorteau.fr>	
#		
# This file is part of QuickScreenShots.		
# 	
# This program is free software; you can redistribute it and/or modify	
# it under the terms of the GNU General Public License as published by		
# the Free Software Foundation; version 3.	
# 	
# This program is distributed in the hope that it will be useful,		
# but WITHOUT ANY WARRANTY; without even the implied warranty of		
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		
# GNU General Public License for more details.		
# 		
# You should have received a copy of the GNU General Public License		
# along with this program; if not, write to the Free Software		
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

from wx import Brush
from PIL import Image, ImageFilter

def dropShadow(image, offset=3, background='#FFFFFF', shadow='#999999', 
               border=5, iterations=5):
  """
  Add a gaussian blur drop shadow to an image.  
  
  image       - The image to overlay on top of the shadow.
  offset      - Offset of the shadow from the image.
  background  - Background colour behind the image.
  shadow      - Shadow colour (darkness).
  border      - Width of the border around the image.  This must be wide
                enough to account for the blurring of the shadow.
  iterations  - Number of times to apply the filter.  More iterations 
                produce a more blurred shadow, but increase processing time.

  Author: Kevin Schluff
  License: Python license
  
  Modified by Clement Lorteau to add shadow all around the picture and use HTML color codes
  """
  
  assert offset >= 0
  b = Brush(background)
  background = b.GetColour().GetRGB()
  b = Brush(shadow)
  shadow = b.GetColour().GetRGB()
    
  # Create the backdrop image -- a box in the background colour with a 
  # shadow on it.
  totalWidth = image.size[0] + offset + 4*border
  totalHeight = image.size[1] + offset + 4*border
  back = Image.new(image.mode, (totalWidth, totalHeight), background)
  
  # Place the shadow, taking into account the offset from the image
  shadowLeft = border + offset
  shadowTop = border + offset
  back.paste(shadow, [shadowLeft, shadowTop, 2*shadowLeft + image.size[0], 
    2*shadowTop + image.size[1]] )
  
  # Apply the filter to blur the edges of the shadow.  Since a small kernel
  # is used, the filter must be applied repeatedly to get a decent blur.
  n = 0
  while n < iterations:
    back = back.filter(ImageFilter.BLUR)
    n += 1
    
  # Paste the input image onto the shadow backdrop  
  imageLeft = border + 2*offset
  imageTop = border + 2*offset
  back.paste(image, (imageLeft, imageTop))
  
  return back

if __name__ == "__main__":
    import time
    import windowscreenshot
    time.sleep(3)
    im = windowscreenshot.snap()
    im = dropShadow(im)
    im.save('screenshot.jpg', 'JPEG')